// components/stars/stars.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    rate:{
      type:Number,
      value:0,
      observer(){
        this.updateRate();
      }
    },
    starSize:{
      type:Number,
      value:20
    },
    fontSize:{
      type:Number,
      value:20
    },
    fontColor:{
      type:String,
      value:"#ccc"
    },
    istext:{
      type:Boolean,
      value:true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    updateRate:function() {
      var that = this;
      var rate = this.properties.rate;
      var intRate = parseInt(rate);
      var light = parseInt(intRate / 2);
      var half = intRate%2;
      var gray = 5 - light - half;
      var lights = [];
      var halfs = [];
      var grays = [];
      for (let index = 1; index <= light; index++) {
        lights.push(index);
      };
      for (let index = 1; index <= half; index++) {
        halfs.push(index);
      };
      for (let index = 1; index <= gray; index++) {
        grays.push(index);
      };
      var rateText = rate && rate > 0 ? rate.toFixed(1) :　"未评分";

      that.setData({
        lights:lights,
        halfs:halfs,
        grays:grays,
        rateText:rateText,
        rate:rate
      })
    }
  },

  lifetimes:{
    attached:function(){
      // console.log(this.properties.rate);
      this.updateRate()
    }
  }
})
